import { observable, action, runInAction, computed } from 'mobx';
import { PublishFetch } from '../../manger/net-fetch';
import PublishApi from '../../manger/net-api';
import { message } from 'antd';
import { AppInfo } from '../../model/app-info';
import { DataSource } from '../componment/table-componment';
class HomeStore {
  // @observable
  appType = 1;

  // @observable
  systemType = 1;

  @observable
  lastAppInfo: AppInfo = null;

  @observable
  appInfoList: AppInfo[] = [];

  @observable
  windowHeight: number = window.innerHeight;

  @observable
  windowWidth: number = window.innerWidth;

  @observable
  modalVisible = false;

  @observable
  qrImgWidth = 80;

  @observable
  loadingStatus = false;

  // header无数据
  @observable
  noData = true;

  // 整个页面loading
  @observable
  isLoading = false;

  // @computed get sliderValue() {
  //     let v1 = this.systemType;
  //     let v2 = this.appType;
  //     this.getAppList();
  //     return v1 + v2;
  // }

  @computed get dataSourceList() {
    let list = this.appInfoList.map((v, i) => {
      let params: DataSource = {};
      params.key = i.toString();
      params.version = v.version;
      params.build = v.build;
      params.size = (v.file.size / 1000 / 1000).toFixed(2) + 'M';
      params.time = v.uploadTime;
      params.filePath = v.file.path;
      params.desc = v.desc;
      params.id = v.id;

      return params;
    });

    //   console.log('newArr',list,this.appInfoList)

    return list;
  }

  @action
  setType(systemType: number, appType: number) {
    // runInAction(()=>{
    //     this.appType = appType;
    //     this.systemType = systemType;
    // })
    this.appType = appType;
    this.systemType = systemType;
    this.getAppList();
  }
  // @action
  // setSystemType(v) {
  //     this.systemType = v;
  // }

  @action
  setQrImgWidth(size: number) {
    runInAction(() => {
      this.qrImgWidth = size;
    });
  }

  @action
  setWindowHeight(height: number) {
    runInAction(() => {
      this.windowHeight = height;
    });
  }
  // 删除app
  @action
  removeAppWithId(id: number) {
    if (!id) {
      return;
    }
    PublishFetch(PublishApi.removeApp + `/${id}`, 'get')
      .then(res => {
        this.getAppList();
      })
      .catch(e => {
        message.error(e.msg || '删除失败');
      });
  }

  @action
  setWindowWidth(width: number) {
    runInAction(() => {
      this.windowWidth = width;
    });
  }

  @action
  setModalVisible(ok: boolean) {
    runInAction(() => {
      this.modalVisible = ok;
    });
  }

  @action
  getAppList() {
    let params = { systemType: this.systemType, appType: this.appType };
    this.isLoading = true;
    PublishFetch(PublishApi.getAppList, 'get', params)
      .then((res: any) => {
        runInAction(() => {
          this.appInfoList = res.data[0];
          this.isLoading = false;
          if (res.data[1] == 0) {
            this.noData = true;
          } else {
            this.noData = false;
            this.lastAppInfo = res.data[0][0];
          }
        });
      })
      .catch(e => {
        this.isLoading = true;
        this.noData = true;
      });
  }

  // 提交app
  @action
  submitApp(params: any) {
    this.loadingStatus = true;
    PublishFetch(PublishApi.submitApp, 'post', params)
      .then(res => {
        runInAction(() => {
          this.loadingStatus = false;
          this.modalVisible = false;
        });
        this.getAppList();
        message.success('提交成功');
      })
      .catch(e => {
        message.success(JSON.stringify(e));
        this.loadingStatus = false;
      });
  }
}

const store = new HomeStore();
export default store;
